package com.xckj.zuoliao.presenter;

import androidx.annotation.NonNull;

import com.xckj.zuoliao.bean.ListBean;
import com.xckj.zuoliao.framework.BaseBean;
import com.xckj.zuoliao.framework.net.RetrofitManager;
import com.xckj.zuoliao.global.Constant;
import com.xckj.zuoliao.iview.IListView;

import java.util.List;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: 代码的搬运工
 * @Describe: 列表
 * @Date: 2024/3/28 23:51
 */
public class ListPresenter {
    private final IListView listView;

    public ListPresenter(IListView listView) {
        this.listView = listView;
    }


    /**
     * 获取列表
     *
     * @param area_type 地区类型
     */
    public void getList(String uid,String area_type) {
        listView.showLoading();
        Call<BaseBean<List<ListBean>>> call = RetrofitManager.getInstance().getService().getList(uid,area_type);
        call.enqueue(new Callback<BaseBean<List<ListBean>>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<List<ListBean>>> call, @NonNull Response<BaseBean<List<ListBean>>> response) {
                if (response.isSuccessful()) {
                    BaseBean<List<ListBean>> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        listView.success(baseBean.data);
                    } else {
                        listView.failed(baseBean.msg);
                    }
                } else {
                    listView.failed("请求失败");
                }
                listView.hideLoading();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<List<ListBean>>> call, @NonNull Throwable t) {
                t.printStackTrace();
                listView.failed("请求失败");
            }
        });
    }
}
